package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.OrganizationMapper;
import cc.rengu.igas.bomp.common.dao.PlatMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.OrganizationMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.PlatMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.Organization;
import cc.rengu.igas.bomp.common.entity.PlatMchntInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.PlatMchntInfoBean;
import cc.rengu.igas.bomp.facade.request.PlatMchntListQueryRequest;
import cc.rengu.igas.bomp.facade.response.PlatMchntListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 平台商户列表查询
 * Created by 赵奎伍
 */
public class PlatMchntListQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        PlatMchntListQueryRequest platMchntListQueryRequest = new PlatMchntListQueryRequest();
        ConvertUtil.convertOutput(platMchntListQueryRequest);
        return platMchntListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        PlatMchntListQueryRequest platMchntListQueryRequest = (PlatMchntListQueryRequest) request;
        PlatMchntListQueryResponse platMchntListQueryResponse = new PlatMchntListQueryResponse();
        BeanUtil.beanCopy(platMchntListQueryRequest, platMchntListQueryResponse);

        String instId = platMchntListQueryRequest.getHeader().getInstId();
        String channelId = platMchntListQueryRequest.getHeader().getChanlId();

        //region 查询条件
        String platMchntNo = platMchntListQueryRequest.getPlatMchntNo();
        String platMchntName = platMchntListQueryRequest.getPlatMchntName();
        String signInstId = platMchntListQueryRequest.getSignInstId();
        int pageIndex = platMchntListQueryRequest.getPageIndex();
        int pageSize = platMchntListQueryRequest.getPageSize();
        rglog.debug("查询条件:platMchntNo<{}>,platMchntName<{}>,signInstId<{}>,pageIndex<{}>,pageSize<{}>", platMchntNo, platMchntName, signInstId, pageIndex, pageSize);
        //endregion

        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);

        PlatMchntInfoMapper platMchntInfoMapper = new PlatMchntInfoMapperImpl();
        List<PlatMchntInfo> platMchntInfoList = new ArrayList<>();

        if (-1 == pageIndex || -1 == pageSize) {
            platMchntInfoList = platMchntInfoMapper.selectPlatMchntInfoByPlatMchntNoOrPlatMchntNameOrSignInstId(instId, platMchntNo, platMchntName, signInstId);
        } else {
            PageResult pageResult = platMchntInfoMapper.selectPlatMchntInfoByPlatMchntNoOrPlatMchntNameOrSignInstIdByPage(instId, platMchntNo, platMchntName, signInstId, pageInfo);
            if (null != pageResult) {
                pageInfo = pageResult.getPageInfo();
                platMchntInfoList = pageResult.getResult().stream().map(item -> (PlatMchntInfo) item).collect(Collectors.toList());
            }
        }

        /*组装报文*/
        if (null != platMchntInfoList && !platMchntInfoList.isEmpty()) {
            List<PlatMchntInfoBean> platMchntInfoBeans = new ArrayList<>();
            platMchntInfoList.forEach(item -> convPlatMchntInfo(item, platMchntInfoBeans));
            /* 查询结果赋值 */
            platMchntListQueryResponse.setTotalRows(pageInfo.getTotalRows());//总记录数
            platMchntListQueryResponse.setTotalPage(pageInfo.getTotalPage());//总页数
            platMchntListQueryResponse.setPlatMchntInfoList(platMchntInfoBeans);
        } else {
            rglog.error("法人行<{}>还未配置平台商户信息!", platMchntListQueryRequest.getHeader().getInstId());
            BizResponse bizResponse = new BizResponse();
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            platMchntListQueryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
            platMchntListQueryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
            if (BompAppParamConstant.MCMP_CHANNEL.equals(channelId)) {
                platMchntListQueryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                platMchntListQueryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
            } else {
                platMchntListQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                platMchntListQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            }
            bizResponse.setResult(platMchntListQueryResponse);
            return bizResponse;

        }
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(platMchntListQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        PlatMchntListQueryResponse platMchntListQueryResponse = (PlatMchntListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(platMchntListQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param platMchntInfo      数据库表结构对象
     * @param platMchntInfoBeans 接口对象
     */
    private void convPlatMchntInfo(PlatMchntInfo platMchntInfo, List<PlatMchntInfoBean> platMchntInfoBeans) {
        //只展示正式的数据  00-非正式   01-正式   02-停用 03-已注销
        PlatMchntInfoBean platMchntInfoBean = new PlatMchntInfoBean();
        BeanUtil.beanCopy(platMchntInfo, platMchntInfoBean);
        platMchntInfoBean.setSignInstId(platMchntInfo.getManageInstId());//签约机构代码

        OrganizationMapper organizationMapper = new OrganizationMapperImpl();
        try {
            Organization organization = organizationMapper.selectOrganizationByPrimaryKey(platMchntInfo.getInstId(), platMchntInfo.getManageInstId());
            if (null != organization) {
                platMchntInfoBean.setSignInstName(organization.getOrgName());//签约机构名称
            }
        } catch (Exception e) {
            rglog.error("获取该机构的签约机构信息失败,该商户代码:<{}>,管理机构代码:<{}>", platMchntInfo.getPlatMchntNo(), platMchntInfo.getManageInstId());
        }

        platMchntInfoBeans.add(platMchntInfoBean);
    }
}
